package com.offcn.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.offcn.page.service.PageService;
import com.offcn.sellergoods.entity.GoodsEntity;
import com.offcn.sellergoods.feign.GoodsFeign;
import com.offcn.sellergoods.feign.ItemCatFeign;
import com.offcn.sellergoods.pojo.Goods;
import com.offcn.sellergoods.pojo.GoodsDesc;
import com.offcn.sellergoods.pojo.Item;
import com.offcn.sellergoods.pojo.ItemCat;
import feign.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private ItemCatFeign itemCatFeign;

    @Value("${pagepath}")
    private String pagepath;

    /**
     * 创建静态页面
     */
    @Autowired
    private TemplateEngine templateEngine;

    public Map<String,Object> buildModel(Long goodsId){
        // 数据模型
        Map<String, Object> dataMap = new HashMap<>();
        // 根据参数 获取对应商品
        GoodsEntity goodsEntity = goodsFeign.findById(goodsId).getData();
        // 拆分数据
        Goods tbGoods = goodsEntity.getTbGoods();
        GoodsDesc tbGoodsDesc = goodsEntity.getTbGoodsDesc();
        List<Item> itemList = goodsEntity.getItemList();

        dataMap.put("tbGoods",tbGoods);
        dataMap.put("tbGoodsDesc",tbGoodsDesc);
        dataMap.put("itemList",itemList);
        List<Map> specList = JSON.parseArray(tbGoodsDesc.getSpecificationItems(), Map.class);
        dataMap.put("specList",specList);
        List<Map> imageList = JSON.parseArray(tbGoodsDesc.getItemImages(), Map.class);
        dataMap.put("imageList",imageList);

        // 获取 一级，二级，三级分类名
        String category1Name = itemCatFeign.findById(tbGoods.getCategory1Id()).getData().getName();
        String category2Name = itemCatFeign.findById(tbGoods.getCategory2Id()).getData().getName();
        String category3Name = itemCatFeign.findById(tbGoods.getCategory3Id()).getData().getName();
        dataMap.put("category1",category1Name);
        dataMap.put("category2",category2Name);
        dataMap.put("category3",category3Name);

        return dataMap;
    }

    @Override
    public void createPageHtml(Long goodsId) {
        // 构建数据模型
        Map<String, Object> dataMap = this.buildModel(goodsId);

        // 上下文
        Context context = new Context();
        context.setVariables(dataMap);

        File dir = new File(pagepath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File htmlFile = new File(dir, goodsId + ".html");

        // 不需要关闭流 ，广泛应用在 IO 中
        try(PrintWriter printWriter = new PrintWriter(htmlFile,"utf-8")){
            // 生成页面
            templateEngine.process("item", context, printWriter);
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }

    //导出函数
    public  void save(String filename,String json) throws IOException {
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8);
        osw.write(json);
        osw.flush();
        osw.close();
    }
}
